package me.fangjian.antunet.lib;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class UserConfig {

	public static final int LIMITATION_CAMPUS = 0xffffffff;
	public static final int LIMITATION_NONE = 0x7fffffff;
	public static final int LIMITATION_DOMESTIC = 0x00000000;

	public static final int LANGUAGE_ENGLISH = 0;
	public static final int LANGUAGE_CHINESE = 1;

	private String szUsername;
	private String szPassword;
	private String md5Username;
	private String md5Password;
	private String szIp;
	private String szAdaptor;
	private String szMac;
	private int limitation;
	private int language;
	private boolean bUseDot1x;
	private boolean bRetryDot1x;

	public void setUsername(String username) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		szUsername = username;
		md5Username = UserConfig.EncoderByMd5(username);
	}

	public void setPassword(String password) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		szPassword = password;
		md5Password = UserConfig.EncoderByMd5(password);
	}

	public void setMD5Password(String szMD5password) {
		md5Password = szMD5password;
	}

	public void setAdapter(String name) {
		szAdaptor = name;
	}

	public void setLimitation(int limitation) {
		this.limitation = limitation;
	}

	public void setLanguage(int language) {
		this.language = language;
	}

	public void setDot1x(boolean usedot1, boolean retrydot1x) {
		bUseDot1x = usedot1;
		bRetryDot1x = retrydot1x;
	}

	public String getSzUsername() {
		return szUsername;
	}

	public String getSzPassword() {
		return szPassword;
	}

	public String getMd5Username() {
		return md5Username;
	}

	public String getMd5Password() {
		return md5Password;
	}

	public String getSzIp() {
		return szIp;
	}

	public String getSzAdaptor() {
		return szAdaptor;
	}

	public String getSzMac() {
		return szMac;
	}

	public int getLimitation() {
		return limitation;
	}

	public int getLanguage() {
		return language;
	}

	public boolean getbUseDot1x() {
		return bUseDot1x;
	}

	public boolean getbRetryDot1x() {
		return bRetryDot1x;
	}

	public static String EncoderByMd5(String str)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		byte[] strTemp = str.getBytes();
		MessageDigest mdTemp = MessageDigest.getInstance("MD5");
		mdTemp.update(strTemp);
		byte[] md = mdTemp.digest();
		int j = md.length;
		char newStr[] = new char[j * 2];
		int k = 0;
		for (int i = 0; i < j; i++) {
			byte b = md[i];
			newStr[k++] = hexDigits[b >> 4 & 0xf];
			newStr[k++] = hexDigits[b & 0xf];
		}
		return new String(newStr);
	}
}
